const http = require('http');
const fs = require('fs');
const path = require('path');
const mime = require('mime');
http.createServer((req,res)=>{
    const fileName = path.join(__dirname,req.url);
    res.setHeader('Content-Type',mime.getType(fileName));
    //注意：/public前面千万不能加点
    if(req.url == "/public/html/index.html"){
        fs.readFile(fileName,(err,data)=>{
            if(!err){
                res.end(data);
            }else{
                console.log(err);
            }
        })
    }else if(req.url == "/public/images/1.jpg"){
        fs.readFile(fileName,(err,data)=>{
            if(!err){
                res.end(data);
            }else{
                console.log(err);
            }
        })
    }
}).listen(3000,err=>{
    if(!err) console.log('服务器已启动成功');
    else console.log(err);
})